# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

pkgbase=libnghttp2
pkgname=("nghttp2" "${pkgbase}" "${pkgbase}-devel")
pkgver=1.39.2
pkgrel=1
pkgdesc='Framing layer of HTTP/2 is implemented as a reusable C library'
arch=(i686 x86_64)
url='https://nghttp2.org/'
depends=("jansson"
         #"jemalloc"
         "libevent"
         "libxml2"
         "openssl"
         "libcares"
         "gcc-libs")
makedepends=('jansson-devel' 'libevent-devel' 'openssl-devel' 'libcares-devel' 'zlib-devel')
license=(MIT)
source=(https://github.com/nghttp2/nghttp2/releases/download/v$pkgver/nghttp2-${pkgver}.tar.xz
        nghttp2-1.24.0-msys2.patch)
sha256sums=('a2d216450abd2beaf4e200c168957968e89d602ca4119338b9d7ab059fd4ce8b'
            '8619c21f76c435ffd8790e4c439038770c73eaca8e23b2d7654067e956a4b86d')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd nghttp2-${pkgver}
  apply_patch_with_msg nghttp2-1.24.0-msys2.patch
  autoreconf -fiv
}

build() {
  cd nghttp2-${pkgver}
  ./configure \
    --prefix=/usr \
    --enable-static=yes \
    --enable-shared=yes \
    --disable-examples \
    --disable-python-bindings
#    --enable-lib-only
  make
  make DESTDIR="${srcdir}/dest" install
}

package_nghttp2() {
  depends=('gcc-libs' 'jansson' "libnghttp2=${pkgver}")
  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
}

package_libnghttp2() {
  pkgdesc='Framing layer of HTTP/2 is implemented as a reusable C library (runtime)'
  depends=('gcc-libs' )
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  install -Dm644 ${srcdir}/nghttp2-${pkgver}/COPYING "${pkgdir}/usr/share/licenses/libnghttp2/COPYING"
}

package_libnghttp2-devel() {
  pkgdesc="nghttp2 headers and libraries"
  options=('staticlibs')
  depends=("libnghttp2=${pkgver}")
  groups=('development')
  depends+=('jansson-devel' 'libevent-devel' 'openssl-devel' 'libcares-devel')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/

}
